let express = require('express');
let router = express.Router();
let mysql = require('mysql');
let connection = mysql.createConnection({
    host : 'localhost',
    user:'root',
    password : 'root',
    database : 'express'
});
connection.connect((err) => {
    if (err) {
        console.log(err)
    }
});

router.post('/register', function (req,response,next)  {
    console.log('req', req.body)
    const {username, password} = req.body;
    connection.query("SELECT * FROM user WHERE username= '" + username + "'", function (err,res,fields) {
        let uName = res[0];
        console.log('res', res)
        if (uName == undefined){
            connection.query('INSERT INTO user SET  ?',{
                username: username,
                password : password,
            },function (err, data) {
                if (err) {
                    console.log('err',err)
                    response.send({
                        status : 100,
                        msg : '注册失败，请重试'
                    });
                    return false
                }else {
                    response.send({
                        status : 200,
                        msg : '注册成功'
                    });
                    return;
                }
            })
        }else {
            response.send({
                statusCode: 0,
                msg: '用户名已存在'
            })
        }
    });
    // next();
});
module.exports = router;
